package com.cooper.base.controller;

import com.cooper.base.model.SysUser;
import com.cooper.base.service.SysUserService;
import com.cooper.base.vo.DataTablesViewPage;
import com.cooper.base.vo.SpMyShiroResult;
import com.cooper.base.vo.SpMyShiroResultBuilder;
import com.cooper.common.core.db.QueryPage;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Optional;

/**
 * Copyright © 2017 com. All rights reserved.
 *
 * @Title: UserCon.java
 * @Prject: springmvc-mybatis-shiro
 * @Package: com.cooper.base.controller
 * @Description: TODO
 * @author: JackCooperZ
 * @date: 2017年1月17日 下午7:51:07
 * @version: V1.0
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 跳转用户列表
     */
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    @RequiresRoles("admin")
    public String list() {
        return "ftl/user/list";
    }

    /**
     * 跳转用户添加
     */
    @RequestMapping(value = "/toAdd", method = {RequestMethod.GET})
    @RequiresRoles("admin")
    public String toAdd() {
        return "ftl/user/add";
    }

    /**
     * 跳转用户编辑
     */
    @RequestMapping(value = "/toEdit/{userId}", method = {RequestMethod.GET,RequestMethod.POST})
    @RequiresRoles("admin")
    public ModelAndView toEdit(@PathVariable String userId, ModelAndView modelAndView) {
        modelAndView.addObject("user", sysUserService.selectByKey(userId));
        modelAndView.setViewName("ftl/user/add");
        return modelAndView;
    }


    /**
     * 添加用户信息
     */
    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    @RequiresRoles("admin")
    @ResponseBody
    public SpMyShiroResult add(SysUser data) {
        try {
            sysUserService.add(data);
            return SpMyShiroResultBuilder.getInstance().success("添加成功");
        } catch (Exception e) {
            return SpMyShiroResultBuilder.getInstance().fail(e.getMessage());
        }
    }

    /**
     * 更新用户信息
     */
    @RequestMapping(value = "/update/{userId}", method = {RequestMethod.POST})
    @RequiresRoles("admin")
    @ResponseBody
    public SysUser update(@PathVariable String userId, SysUser data) {
        SysUser sysUser = sysUserService.selectByKey(userId);
        return sysUserService.update(sysUser, data);
    }

    @RequestMapping(value = "/validate", method = {RequestMethod.POST})
    @RequiresRoles("admin")
    @ResponseBody
    public SpMyShiroResult validate(SysUser data) {
        SysUser sysUser = sysUserService.selectOne(data);
        if (Optional.ofNullable(sysUser).isPresent()) {
            return SpMyShiroResultBuilder.getInstance().fail(String.format("用户名%s已存在", data.getUserName()));
        }
        return SpMyShiroResultBuilder.getInstance().success();
    }


    /**
     * 用户列表分页
     */
    @RequestMapping(value = "/ajaxList", method = {RequestMethod.POST})
    @RequiresRoles("admin")
    @ResponseBody
    public DataTablesViewPage ajaxList(String sEcho, QueryPage queryPage) {
        return new DataTablesViewPage(sysUserService.list(queryPage), queryPage, sEcho);
    }

}
